package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysRole;
import cn.knightzz.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javassist.tools.rmi.ObjectNotFoundException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-04-09
 */
@Api("角色管理")
@RestController
public class SysRoleController {

    @Resource
    SysRoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping("/roles")
    public JsonResult<SysRole> addRole(@RequestBody SysRole role) throws ObjectNotFoundException {
        boolean status = roleService.addRole(role);
        JsonResult<SysRole> result;
        if (status) {
            // 角色添加成功
            result = new JsonResult<>("200", "角色添加成功!");
            result.setData(role);
        } else {
            result = new JsonResult<>("201", "角色添加失败!");
        }
        return result;
    }

    @ApiOperation("根据角色id获取角色信息")
    @GetMapping("/roles/{id}")
    public JsonResult<SysRole> getRoleByRoleId(@PathVariable Integer id) {
        SysRole role = roleService.getRoleById(id);
        JsonResult<SysRole> result;
        if (ObjectUtil.isNull(role)) {
            // 查询失败, 返回结果为null
            result = new JsonResult<>("201", "角色获取失败!");
        } else {
            // 查询成功
            result = new JsonResult<>("200", "角色获取成功");
            result.setData(role);
        }
        return result;
    }

    @ApiOperation("分页获取角色列表")
    @GetMapping("/roles")
    public JsonResult<Map<String, Object>> selectRolePage(@RequestParam("pagenum") Long pageNum,
                                                       @RequestParam("pagesize") Long pageSize,
                                                       @RequestParam("query") String query) {
        IPage<SysRole> sysRoleIPage = roleService.selectRolePage(pageSize, pageNum, query);
        // 获取总数据条数
        long total = sysRoleIPage.getTotal();
        // 获取页码
        long current = sysRoleIPage.getCurrent();
        // 获取角色列表
        List<SysRole> roles = sysRoleIPage.getRecords();
        // 封装响应数据
        JsonResult<Map<String, Object>> result = new JsonResult<>("200", "角色列表获取成功!");
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("current", current);
        data.put("roles", roles);
        result.setData(data);
        return result;
    }

    @ApiOperation("获取角色列表")
    @GetMapping("/roles/list/{id}")
    public JsonResult<List<SysRole>> getRoleList(@PathVariable Long id) {
        List<SysRole> roleList = roleService.getRoleList(id);
        JsonResult<List<SysRole>> result ;
        if(roleList.isEmpty()){
            result = new JsonResult<>("201" , "角色列表为空!");
        }else {
           result = new JsonResult<>("200" , "角色列表获取成功!");
        }
        result.setData(roleList);
        return result;
    }

    // 更新角色信息
    @ApiOperation("更新角色信息")
    @PutMapping("/roles")
    public JsonResult<SysRole> updateRole(@RequestBody SysRole role) {
        Boolean status = roleService.updateRole(role);
        JsonResult<SysRole> result;
        if (status) {
            // 角色更新成功
            result = new JsonResult<>("200", "角色更新成功!");
            result.setData(role);
        } else {
            result = new JsonResult<>("201", "角色更新失败!");
        }
        return result;
    }

    @DeleteMapping("/roles/{id}")
    public JsonResult<SysRole> deleteRole(@PathVariable Long id) {
        Boolean status = roleService.deleteRole(id);
        JsonResult<SysRole> result;
        if (status) {
            // 角色更新成功
            result = new JsonResult<>("200", "角色删除成功!");
        } else {
            result = new JsonResult<>("201", "角色删除失败, 该角色!");
        }
        return result;
    }

    /**
     * 根据角色编码查询角色信息
     *
     * @param roleCode
     * @return
     */
    @GetMapping("/roles/code/{roleCode}")
    public JsonResult<SysRole> selectRoleByRoleCode(@PathVariable("roleCode") String roleCode) {
        SysRole role = roleService.getRoleByRoleCode(roleCode);
        JsonResult<SysRole> result;
        if (ObjectUtil.isNull(role)) {
            // 角色为null , 说明 未查询到该角色
            result = new JsonResult<>("201", "未查询到该角色");
        } else {
            result = new JsonResult<>("200", "已查询到该角色");
            result.setData(role);
        }
        return result;
    }

    /**
     * 根据角色名查询角色信息
     *
     * @param roleName
     * @return
     */
    @GetMapping("/roles/name/{roleName}")
    public JsonResult<SysRole> selectRoleByRoleName(@PathVariable("roleName") String roleName) {
        SysRole role = roleService.getRoleByRoleName(roleName);
        JsonResult<SysRole> result;
        if (ObjectUtil.isNull(role)) {
            // 角色为null , 说明 未查询到该角色
            result = new JsonResult<>("201", "未查询到该角色");
        } else {
            result = new JsonResult<>("200", "已查询到该角色");
            result.setData(role);
        }
        return result;
    }
}

